namespace BM.Tools.WebControls.MForm
{
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;        
    using System.Web.UI;
    
    using BM.Tools.WebControls.MForm.Controls;    
    using BM.Tools.WebControls.MForm.ScriptReferences;    

    /// <summary>
    /// A class that helps the <see cref="Root" /> control with registering scripts.
    /// </summary>
    public class RootScriptHelper
    {        
        /// <summary>
        /// Initializes a new instance of the <see cref="RootScriptHelper"/> class. 
        /// </summary>
        /// <param name="root">
        /// The root control.
        /// </param>
        public RootScriptHelper(Root root)
        {
            Root = root;
        }

        /// <summary>
        /// Gets the control on which the the helper works.
        /// </summary>
        /// <value>
        /// The root control.
        /// </value>
        public Root Root
        {
            get;
            private set;
        }        

        /// <summary>
        /// Adds all resources and scripts associated with the root control.
        /// </summary>
        public void RegisterScripts()
        {
            ScriptIncluder.IncludeResources(Root);
            ScriptManager sm = ScriptManager.GetCurrent(Root.Page);
            if (sm == null)
            {
                return;
            }
                      
            sm.RegisterScriptControl(Root);             
        }

        /// <summary>
        /// Performs the actions that should be done on control render.
        /// </summary>
        public void Render()
        {
            ScriptManager sm = ScriptManager.GetCurrent(Root.Page);
            if (sm == null)
            {
                return;
            }            

            sm.RegisterScriptDescriptors(Root);           
        }

        /// <summary>
        /// Implements the method from interface <see cref="IScriptControl" />.
        /// </summary>
        /// <returns>
        /// the enumeration of <see cref="ScriptDescriptor" /> obejcts
        /// </returns>
        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "The method is used, becuase it accomplishes the goals of the IScriptControl method GetScriptDescriptors")]
        public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            yield return GetRootDescriptor();
            foreach (var key in Root.PrototypeDescriptors.Keys)
            {
                yield return Root.PrototypeDescriptors[key];
            }
        }

        [SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "The lowercase characters are needed by javascript.")]
        private ScriptDescriptor GetRootDescriptor()
        {
            var descriptor = new ScriptControlDescriptor("MForm", Root.ClientID);
            descriptor.AddProperty("imagesPath", ScriptIncluder.MapPath(false, Root.Page, Root.CommonImagesPath));                        
            descriptor.AddProperty("mode", Root.Mode.ToString().ToLower(CultureInfo.InvariantCulture));            
            descriptor.AddProperty("shouldValidateInViewMode", Root.ShouldValidateInViewMode);
            descriptor.AddProperty("idSeparator", Root.Page.IdSeparator);
            descriptor.AddProperty("culturalContext", Root.CulturalContext);
            return descriptor;
        }
    }
}